package com.zhuang.chapter19;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManagerSystem {
	public static Scanner sc = new Scanner(System.in);

	// 创建集合对象，用于存储学生数据
	static ArrayList<Student> array = new ArrayList<Student>();

	public static void main(String[] args) {
		// 用Scanner实现键盘录入数据
		boolean flag = true;
		if (flag) {
			System.out.println("----欢迎来到学校管理系统----");
			// 用循环完成再次回到主界面
			while (true) {
				System.out.println("请根据对应序号进行选择");
				System.out.println("1 添加学生");
				System.out.println("2 删除学生");
				System.out.println("3 修改学生");
				System.out.println("4 查看学生");
				System.out.println("5 退出");
				System.out.println("请输入你的选择：");
				String line = sc.nextLine();
				// 用switch语句完成操作的选择
				switch (line) {
				case "1":
					add(new Student());
					break;
				case "2":
					remove();
					break;
				case "3":
					set(new Student());
					break;
				case "4":
					showAllStudent(array);
					break;
				case "5":
					System.out.println("谢谢使用");
					System.exit(0); // JVM退出
				default:
					System.out.println("没有对应的操作,请重新选择");
				}
			}
		}
	}

	// 删除学生
	private static void remove() {
		System.out.println("输入要删除的学号：");
		String id = sc.nextLine();
		remove(getIndex(id));
	}

	// 根据索引删除学生
	private static void remove(int index) {
		if (index == -1) {
			System.out.println("学生信息不存在，返回主页面！");
			return;
		} else {
			array.remove(index);
			System.out.println("删除学生成功！");
		}
	}

	// 根据学号找到指定的索引
	private static int getIndex(String id) {
		int index = 0;
		for (int i = 0; i < array.size(); i++) {
			if (array.get(i).getSid().equals(id)) {
				index = i;
				return index;
			}
		}
		return -1;
	}

	// 添加学生
	public static void add(Student student) {
		add(array.size(), student);
	}

	// 根据索引添加学生
	public static void add(int index, Student student) {
		String id = "";
		while (true) {
			System.out.print("输入学号：");
			id = sc.nextLine();
			boolean flag = false;
			for (int i = 0; i < array.size(); i++) {
				student = array.get(i);
				if (id.equals(student.getSid())) {
					flag = true;
					break;
				}
			}
			if (flag) {
				System.out.println("学号已被使用，重新输入！");
			} else {
				break;
			}
		}
		System.out.print("请输入姓名：");
		String name = sc.nextLine();
		System.out.print("请输入年龄：");
		String age = sc.nextLine();
		System.out.print("请输入生日：");
		String birthday = sc.nextLine();
		student = new Student(id, name, age, birthday);
		array.add(index, student);
		System.out.println("添加学生成功！");

	}

	// 更新学生
	public static void set(Student student) {
		String id = "";
		System.out.println("输入要更新的学号：");
		id = sc.nextLine();
		for (int i = 0; i < array.size(); i++) {
			if (array.get(i).getSid().equals(id)) {
				student = array.get(i);
			}
		}
		System.out.print("请输入姓名：");
		String name = sc.nextLine();
		System.out.print("请输入年龄：");
		String age = sc.nextLine();
		System.out.print("请输入生日：");
		String birthday = sc.nextLine();
		student = new Student(id, name, age, birthday);
		array.set(getIndex(id), student);
		System.out.println("更新学生成功");
	}

	// 查看学生
	private static void showAllStudent(ArrayList<Student> array) {
		if (array.size() == 0) {
			System.out.println("无学生信息，请重新选择");
			return;
		}
		System.out.println();
		System.out.println("学号\t\t\t\t姓名\t\t\t\t年龄\t\t\t生日");
		System.out.println("------------");
		for (int x = 0; x < array.size(); x++) {
			Student s = array.get(x);
			System.out.println(s.getSid() + "\t\t\t\t" + s.getName() + "\t\t\t\t" + s.getAge() + "\t\t\t\t"
					+ s.getBirthday() + "\t\t\t\t");
		}
		System.out.println("------------");
	}
}
